package com.sunronsoft.mind.map;

import com.jfinal.core.Controller;
import com.jfinal.kit.*;
import com.jfinal.plugin.ehcache.CacheKit;
import com.sunronsoft.mind.map.model.User;

/**
 * @author Sunron
 **/
public class UserController extends Controller {
    
    private static UserService userService = UserService.getInstance();
    
    public void save() {
        Kv params = JsonKit.parse(getRawData(), Kv.class);
        if (params == null || params.isEmpty()) {
            renderJson(DataKit.toFail("参数错误"));
        } else if (StrKit.isBlank(params.getStr("userName")) || StrKit.isBlank(params.getStr("password"))) {
            renderJson(DataKit.toFail("参数错误"));
        } else {
            try {
                if (userService.save(params.getStr("userName"), params.getStr("password"))) {
                    renderJson(DataKit.toSuccess("数据保存成功"));
                } else {
                    renderJson(DataKit.toFail("数据保存失败"));
                }
            } catch (Exception e) {
                LogKit.error(e.getMessage(), e);
                renderJson(DataKit.toFail("数据保存失败"));
            }
        }
    }
    
    public void reset() {
        Kv params = JsonKit.parse(getRawData(), Kv.class);
        if (params == null || params.isEmpty()) {
            renderJson(DataKit.toFail("参数错误"));
        } else {
            String oldPassword = params.getStr("oldWord");
            String newPassword = params.getStr("newWord");
            
            if (StrKit.isBlank(oldPassword)) {
                renderJson(DataKit.toFail("请输入旧密码"));
            } else if (StrKit.isBlank(newPassword) || newPassword.trim().length() < 6 || newPassword.trim().length() > 15) {
                renderJson(DataKit.toFail("新密码长度为6到15个数字或字母"));
            } else {
                try {
                    Kv cache = CacheKit.get("authorizationCache", getSession().getId());

                    if (null == cache) {
                        renderJson(DataKit.toFail("获取数据失败"));
                    } else {
                        oldPassword = HashKit.md5(oldPassword.trim());
                        User user = userService.detail(cache.getStr("userName"));
                        if (null == user) {
                            renderJson(DataKit.toFail("获取数据失败"));
                        } else if (!user.getPassword().equals(oldPassword)) {
                            renderJson(DataKit.toFail("旧密码错误"));
                        } else {
                            userService.save(user.getUserName(), newPassword);

                            renderJson(DataKit.toSuccess("密码修改成功"));
                        }
                    }
                } catch (Exception e) {
                    LogKit.error(e.getMessage(), e);
                    renderJson(DataKit.toFail("修改密码失败"));
                }
            }
        }
    }
    
    public void delete() {
        Kv params = JsonKit.parse(getRawData(), Kv.class);
        if (params == null || params.isEmpty()) {
            renderJson(DataKit.toFail("参数错误"));
        } else if (!params.containsKey("userName")) {
            renderJson(DataKit.toFail("参数错误"));
        } else if (userService.delete(params.getStr("userName"))) {
            renderJson(DataKit.toSuccess("删除管理员成功"));
        } else {
            renderJson(DataKit.toFail("删除管理员失败"));
        }
    }
    
    public void list() {
        renderJson(DataKit.toSuccess(userService.list()));
    }
}
